/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var MONSTER= function(x, y) {
    this.buffer= "";
    this.ctx= "";
    this.state="IDLE"; // IDLE / WALK / AGGRO / ATK
    this.aCd = 15;
    this.anim = false;
    this.iAnim = 20;
    this.pAnim = "idle";
    
    this.move= false;
    this.aggro=false;
    this.atk= false;
    this.back= false;
    this.idle=true;
    this.dir = false;
    
    this.grav = 0;
    this.spd = 10;
    this.axel = 0;
    
    this.pos= {
        X: x,
        Y: y,
    };
    
    this.size= 36;
    this.sprite= "";
    
    this.Init= function() {
        this.buffer = document.createElement("canvas");
        this.buffer.height = this.buffer.width = this.size;
        
        this.ctx = this.buffer.getContext("2d");
        
        this.sprite = new Image();
        this.sprite.src = "RES/Monster.png";
    };
    
    this.Update= function() {
        /*
         * IA:
         * IDLE/WALK/AGGRO/ATK
         */
        if (this.pos.X+this.size/2 < 0) this.pos.X = WIDTH+this.size/2;
        this.Anim(this.AI());
    };
    
    this.Draw= function(ctx) {
        this.ctx.drawImage(this.sprite, 0, 0);
        ctx.drawImage(this.buffer, this.pos.X-this.size/2, HEIGHT-FLOOR-this.pos.Y-this.size);
    }
    
    this.AI = function() {        
        var dist = (Math.sqrt(Math.pow(this.pos.X-CHAR.pos.X, 2)));
        /*
         * base state : idle
         *               ]                 ]
         * ran -> walk   |                 |
         *     -> idle  _|                 |
         *                                 |
         * si dist d'aggro : ran -> aggro  |
         *    |                  -> _______|
         *    |-> quand dist d'atk : atk puis cd <-]
         *    
         *    renvoie l'état
         */
        
        if (Math.random() < 0.8 && !this.move && !this.aggro && !this.atk){
            this.move=true;
            this.idle=false;
        }
        
        if (this.move && !this.aggro && !this.atk) {
            if (Math.random() > dist/(WIDTH*0.7)) {
                this.move = false;
                this.aggro = true;
            } else return "move";
        }
        
        if (this.aggro && !this.atk) {
            if (dist < 50) {
                this.aggro = false;
                this.atk = true;
                return "atk";
            } else {
                return "aggro";
            }
        }
        
        if (this.atk) {
            if (dist >= 50) {
                this.atk = false;
                return "aggro";
            } else return "atk";
        } else {
            return "idle";
        }
    }
    
    this.Anim = function(state) {
        //        console.log(state);
        if (this.iAnim == 20 && !this.anim) this.pAnim = state;
        if (this.iAnim > 0 && this.anim) state = this.pAnim;
        switch (state) {
            case "idle":
                if (this.iAnim < 0) {
                    this.iAnim = 20;
                    this.idle = false;
                    this.anim = false;
                } else {
                    this.anim = true;
                    console.log("Je fais rien");
                    this.iAnim--;
                }
                break;
            case "move":
                if (this.iAnim < 0) {
                    this.iAnim = 20;
                    this.move = false;
                    this.anim = false;
                } else {
                    this.anim = true;
                    console.log("Jme Ballade =3");
                    this.iAnim--;
                    this.pos.X-=2;
                }
                break;
            case "aggro":
                if (this.iAnim < 0) {
                    this.iAnim = 20;
                    this.anim = false;
                } else {
                    this.anim = true;
                    console.log("Je t'ai vu !!");
                    this.iAnim--;
                    this.pos.X-=3;
                }
                break;
            case "atk":
                this.atk = false;
                console.log("JVAIS TMARAVe !!");
                break;
        }
    }
}
